// pages/library/library.js
var util = require('../../utils/util.js');
const config = require('../../utils/config');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bookLists: [],
    searchWord:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: config.libraryUrl + '/reader_books',
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        
        console.log( res.data)
        var bookLists = res.data;
        if(bookLists != null){
          wx.hideLoading();
          for(var i=0; i<bookLists.length; i++){
            bookLists[i].pubdate = util.formatTimeTwo(bookLists[i].pubdate,'Y-M-D'); 
          }
          that.setData({
            bookLists:  bookLists,
          })
        } else{
          wx.hideLoading();
        }
      },fail(){
        wx.hideLoading();
        wx.showToast({
          title: '系统错误',
          image: '/images/erron.png',
          duration: 2000,
        })
      },complete(){
        wx.hideLoading();
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //获取Input数据
  searchWord:function(e){
    this.setData({
      searchWord: e.detail.value
    })
  },

  // 图书搜索
  bookSearch:function(e){
    var that = this;
    wx.showLoading({
      title: '搜索中...',
    });
    wx.request({
      url: 'http://localhost:8080/library/reader_querybook_do',
      data: {
        searchWord: that.data.searchWord
      },
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var searchLists = res.data;
        console.log(searchLists);
        if(searchLists != null){
          that.setData({
            bookLists: searchLists
          });
          wx.hideLoading();
        } else {
          wx.hideLoading();
        }
      }
    })
  }

})